package com.mobvoi.serialport.controller;

import android.util.Log;


public class RobotActionController {

    private static RobotActionController INSTANCE;
    private RosCallbackParser parser;



    public static RobotActionController getInstance() {
        if (INSTANCE == null) {
            synchronized (RobotActionController.class) {
                if (INSTANCE == null) {
                    INSTANCE = new RobotActionController();
                }
            }
        }
        return INSTANCE;
    }


    /**
     * 默认的初始化串口方法,波特率:115200,串口地址:/dev/ttyS1
     *
     * @param callback ROS上报内容的回调
     * @param path     要上传到ros的日志目录
     * @throws Exception
     */
    public void init(RosCallbackParser.RosCallback callback, String... path) throws Exception {
        parser = new RosCallbackParser.Builder()
                .baudRate(115200)
                .port("/dev/ttyS1")
                .callback(callback)
                .build();
        startListen(path);
    }


    /**
     * @param baudRate 波特率
     * @param port     串口地址
     * @param callback ROS上报内容的回调
     * @param path     要上传到ros的日志目录
     * @throws Exception
     */
    public void init(int baudRate, String port, RosCallbackParser.RosCallback callback, String... path) throws Exception {
        parser = new RosCallbackParser.Builder()
                .baudRate(baudRate)
                .port(port)
                .callback(callback)
                .build();
        Log.d("uartservice","baudRate: " + baudRate + ",port: " + port+ "parser:"+parser);
        startListen(path);
    }

    private void startListen(String... path) throws Exception {
        Log.d("uartservice","control start listen");
        parser.startListen();
    }

    public void stopListen() {
        Log.d("uartservice","control stop listen");
        if (parser != null) {
            parser.stopListen();
            parser = null;
        }
        INSTANCE = null;
    }

    /**
     * 发送指令到导航,异步返回结果
     *
     * @param hexstr
     * @return 返回结果在RobotActionController.onResult(String)
     * @see com.mobvoi.serialport.controller.RosCallbackParser.RosCallback#(String)
     */
    public void sendCommand(String hexstr) {
        Log.d("wzb","s5 send:"+hexstr);
        parser.sendCommand(hexstr);
    }

    public void sendCommandToQueue(String hexstr) {
        parser.sendCommandToQueue(hexstr);

    }



}
